const {
    logger,
    sleep,
    saveScreenshot,
    checkCaptcha,
    SendSms,
} = require('./util');

exports.preparePay = async page => {
    try {
        logger('准备结算');

        //跳转到结算页
        for (let i=0; i<3; i++) {
            try {
                await page.goto('https://www.ti.com.cn/store/ti/zh/cart');
                await page.waitForSelector('th.order_total', {
                    timeout: 30000
                });
                break;
            } catch(e) {
                logger('未打开结算页，重试');
            }
        }
        const checkOrderTotal = await page.$('th.order_total');
        if (checkOrderTotal) {
            logger('已进入结算页');
        } else {
            throw new Error('打开结算页失败');
        }
        await sleep(5000);

        //检查支付货币是否CNY
        for (let i=0; i<3; i++) {
            try {
                const txt = await page.evaluate(() => document.querySelector('th.order_total').innerText);
                if (txt.indexOf('CNY') < 0) {
                    logger('点击货币');
                    const dropdown = await page.evaluateHandle(() => {
                        return document.querySelector("#tiResponsiveHeader ti-header-currency-selection").shadowRoot.querySelector("ti-dropdown-panel").shadowRoot.querySelector(".ti-dropdown-panel-trigger");
                    });
                    await dropdown.click();
                    await sleep(1000);

                    logger('选择CNY');
                    const cny = await page.evaluateHandle(() => {
                        return document.querySelector("#tiResponsiveHeader ti-header-currency-selection").shadowRoot.querySelector("ti-dropdown-panel li[data-value=\"CNY\"] a");
                    });
                    await cny.click();
                    await sleep(1000);

                    logger('等待页面跳转');
                    await page.waitForFunction(() => {
                        return document.querySelector('th.order_total') && document.querySelector('th.order_total').innerText.indexOf('CNY') > -1;
                    }, {
                        timeout: 20000
                    });
                }
            } catch(e) {
                await sleep(3000);
            }
        }
        logger('继续');
        await sleep(10000);

        logger('等待跳转到支付页面');
        for (let i=0; i<3; i++) {
            try {
                await page.goto('https://www.ti.com.cn/store/ti/zh/cart/checkout');
                await page.waitForSelector('.addressType', {
                    timeout: 60000
                });
                break;
            } catch(e) {
                logger('失败，重试');
            }
        }
        await sleep(2000);

    } catch(e) {
        logger('下单前准备出错', page.url(), e);
        await saveScreenshot(page, 'prepare');
        await checkCaptcha(page);
        SendSms({
            TemplateId: '1169784',
            TemplateParamSet:[
                '结算', '下单前准备出错'
            ]
        }, 'dev');
        return false;
    }

    return true;
}
